<?php


namespace app\api\controller;


use app\admin\model\ForeknowledgeCollection;
use app\admin\model\LargerActiveCollection;
use app\admin\model\ListContent;
use app\admin\model\RecordedCollection;
use app\admin\model\SelectedCollection;
use think\Controller;
use think\Request;

class Collection extends Controller
{
    public function getCommonUserCollection(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $collection = new \app\admin\model\Collection();

        $where['user_id'] = $data['user_id'];
        $rs = $collection->getAll($where);
        $collectionArray = [];
        $collectionArray['foreknowledge'] = [];
        $collectionArray['recorded'] = [];
        $collectionArray['largerActive'] = [];
        $collectionArray['selected'] = [];
        if($rs){
            foreach ($rs as $k=>$v){
                if($v['type'] == 'foreknowledge'){
                    $collectionArray['foreknowledge'][] = $v;
                }
                if($v['type'] == 'recorded'){
                    $collectionArray['recorded'][] = $v;
                }
                if($v['type'] == 'largerActive'){
                    $collectionArray['largerActive'][] = $v;
                }
                if($v['type'] == 'selected'){
                    $collectionArray['selected'][] = $v;
                }
            }
            $res['data']['code'] = 1;
            $res['data']['list'] = $collectionArray;
        }else{
            if(count($rs) == 0){
                $res['data']['code'] = 1;
                $res['data']['list'] = $collectionArray;
            }else{
                $res['data']['code'] = 0;
            }
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function getUserCollection(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['type'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }
        $where['user_id'] = $data['user_id'];

        switch ($data['type'])
        {
            //3D预知现场
            case 'foreknowledge':
                $collection = new ForeknowledgeCollection();
            break;
            //录制活动
            case 'recorded':
                $collection = new RecordedCollection();
            break;
            //大型活动
            case 'largerActive':
                $collection = new LargerActiveCollection();
            break;
            //酒店
            case 'selected':
                $collection = new SelectedCollection();
            break;
            default:
                $res['status'] = 400;
                echo json_encode($res);die;
        }
        $rs = $collection->getAll($where);

        if($rs){
            $res['data']['code'] = 1;
            $res['data']['list'] = $rs;
        }else{
            $res['data']['code'] = 0;
        }

        $res['status'] = 200;
        echo json_encode($res);
    }

    public function addCommonCollection(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['type']) or !isset($data['p_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $user = new \app\admin\model\User();
        $userWhere['id'] = $data['user_id'];
        $isUser = $user->getOne($userWhere);
        if(!$isUser){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '暂无此用户';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        switch ($data['type'])
        {
            //3D预知现场
            case 'foreknowledge':
                $activeDb = new \app\admin\model\Foreknowledge();
                break;
            //录制活动
            case 'recorded':
                $activeDb = new \app\admin\model\Recorded();
                break;
            //大型活动
            case 'largerActive':
                $activeDb = new \app\admin\model\LargerActive();
                break;
            //酒店
            case 'selected':
                $activeDb = new ListContent();
                break;
            default:
                $res['status'] = 400;
                echo json_encode($res);die;
        }
        $where['id'] = $data['p_id'];

        $rs = $activeDb->getOne($where);

        if(!$rs){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '没有此产品信息';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        $collection = new \app\admin\model\Collection();
        $checkWhere['user_id'] = $data['user_id'];
        $checkWhere['type'] =  $data['type'];
        $checkWhere['p_id'] =  $data['p_id'];
        if($collection->getOne($checkWhere)){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '已收藏';
        }else{
            $save['user_id'] = $data['user_id'];
            $save['type'] = $data['type'];
            $save['p_id'] = $data['p_id'];
            if(!isset($rs['name'])){
                $save['name'] = $rs['title'];
            }else{
                $save['name'] = $rs['name'];
            }
            if(!isset($rs['img'])){
                $save['image'] = $rs['image'];
            }else{
                $save['image'] = $rs['img'];
            }
            if($data['type'] != 'selected'){
                $save['video'] = $rs['video'];
            }
            $save['c_time'] = time();
            $addCollection = $collection->add($save);
            if($addCollection){
                $res['data']['code'] = 1;
                $res['data']['msg'] = '收藏成功';
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '收藏失败';
            }
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function addCollection(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['type']) or !isset($data['id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $user = new \app\admin\model\User();
        $userWhere['id'] = $data['user_id'];
        $isUser = $user->getOne($userWhere);
        if(!$isUser){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '暂无此用户';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        switch ($data['type'])
        {
            //3D预知现场
            case 'foreknowledge':
                $foreknowledge = new \app\admin\model\Foreknowledge();
                $where['id'] = $data['id'];

                $rs = $foreknowledge->getOne($where);
                if(!$rs){
                    $res['data']['code'] = 0;
                    $res['data']['msg'] = '没有此信息';
                    $res['status'] = 200;
                    echo json_encode($res);die;
                }
                $checkWhere['user_id'] = $data['user_id'];
                $checkWhere['f_id'] = $rs['id'];

                $collection = new ForeknowledgeCollection();
                $save['f_id'] = $rs['id'];
                $save['f_name'] = $rs['title'];
                $save['f_image'] = $rs['img'];
                break;
            //录制活动
            case 'recorded':
                $recorded = new \app\admin\model\Recorded();
                $where['id'] = $data['id'];

                $rs = $recorded->getOne($where);
                if(!$rs){
                    $res['data']['code'] = 0;
                    $res['data']['msg'] = '没有此信息';
                    $res['status'] = 200;
                    echo json_encode($res);die;
                }
                $checkWhere['user_id'] = $data['user_id'];
                $checkWhere['r_id'] = $rs['id'];

                $collection = new RecordedCollection();
                $save['r_id'] = $rs['id'];
                $save['r_name'] = $rs['name'];
                $save['r_image'] = $rs['img'];
                break;
            //大型活动
            case 'largerActive':
                $largerActive = new \app\admin\model\LargerActive();
                $where['id'] = $data['id'];

                $rs = $largerActive->getOne($where);
                if(!$rs){
                    $res['data']['code'] = 0;
                    $res['data']['msg'] = '没有此信息';
                    $res['status'] = 200;
                    echo json_encode($res);die;
                }
                $checkWhere['user_id'] = $data['user_id'];
                $checkWhere['l_id'] = $rs['id'];

                $collection = new LargerActiveCollection();
                $save['l_id'] = $rs['id'];
                $save['l_name'] = $rs['name'];
                $save['l_image'] = $rs['img'];
                break;
            default:
                $res['status'] = 400;
                echo json_encode($res);die;
        }

        if($collection->getOne($checkWhere)){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '已收藏';
        }else{
            $save['user_id'] = $data['user_id'];
            $save['c_time'] = time();
            $addCollection = $collection->add($save);
            if($addCollection){
                $res['data']['code'] = 1;
                $res['data']['msg'] = '收藏成功';
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '收藏失败';
            }
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function delCommonCollection(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['type']) or !isset($data['p_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $user = new \app\admin\model\User();
        $userWhere['id'] = $data['user_id'];
        $isUser = $user->getOne($userWhere);
        if(!$isUser){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '暂无此用户';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        switch ($data['type'])
        {
            //3D预知现场
            case 'foreknowledge':
                $activeDb = new \app\admin\model\Foreknowledge();
                break;
            //录制活动
            case 'recorded':
                $activeDb = new \app\admin\model\Recorded();
                break;
            //大型活动
            case 'largerActive':
                $activeDb = new \app\admin\model\LargerActive();
                break;
            //酒店
            case 'selected':
                $activeDb = new ListContent();
                break;
            default:
                $res['status'] = 400;
                echo json_encode($res);die;
        }
        $where['id'] = $data['p_id'];

        $rs = $activeDb->getOne($where);

        if(!$rs){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '没有此产品信息';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        $collection = new \app\admin\model\Collection();
        $checkWhere['user_id'] = $data['user_id'];
        $checkWhere['type'] =  $data['type'];
        $checkWhere['p_id'] =  $data['p_id'];
        if(!$collection->getOne($checkWhere)){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '此产品未收藏';
        }else{
            $del['user_id'] = $data['user_id'];
            $del['type'] = $data['type'];
            $del['p_id'] = $data['p_id'];
            $addCollection = $collection->del($del);
            if($addCollection){
                $res['data']['code'] = 1;
                $res['data']['msg'] = '取消收藏';
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '取消失败';
            }
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function delCollection(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['type']) or !isset($data['id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $user = new \app\admin\model\User();
        $userWhere['id'] = $data['user_id'];
        $isUser = $user->getOne($userWhere);
        if(!$isUser){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '暂无此用户';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        switch ($data['type'])
        {
            //3D预知现场
            case 'foreknowledge':
                $foreknowledge = new \app\admin\model\Foreknowledge();
                $where['id'] = $data['id'];

                $rs = $foreknowledge->getOne($where);
                if(!$rs){
                    $res['data']['code'] = 0;
                    $res['data']['msg'] = '没有此信息';
                    $res['status'] = 200;
                    echo json_encode($res);die;
                }
                $delWhere['user_id'] = $data['user_id'];
                $delWhere['f_id'] = $rs['id'];

                $collection = new ForeknowledgeCollection();
                break;
            //录制活动
            case 'recorded':
                $recorded = new \app\admin\model\Recorded();
                $where['id'] = $data['id'];

                $rs = $recorded->getOne($where);
                if(!$rs){
                    $res['data']['code'] = 0;
                    $res['data']['msg'] = '没有此信息';
                    $res['status'] = 200;
                    echo json_encode($res);die;
                }
                $delWhere['user_id'] = $data['user_id'];
                $delWhere['r_id'] = $rs['id'];

                $collection = new RecordedCollection();
                break;
            //大型活动
            case 'largerActive':
                $largerActive = new \app\admin\model\LargerActive();
                $where['id'] = $data['id'];

                $rs = $largerActive->getOne($where);
                if(!$rs){
                    $res['data']['code'] = 0;
                    $res['data']['msg'] = '没有此信息';
                    $res['status'] = 200;
                    echo json_encode($res);die;
                }
                $delWhere['user_id'] = $data['user_id'];
                $delWhere['l_id'] = $rs['id'];

                $collection = new LargerActiveCollection();
                break;
            default:
                $res['status'] = 400;
                echo json_encode($res);die;
        }
        if($collection->del($delWhere)){
            $res['data']['code'] = 1;
            $res['data']['msg'] = '取消成功';
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '取消失败';
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

}